﻿using System;
using System.Web;
using Nexia.FluentHtmlElements.Html;

namespace Nexia.FluentHtmlElements.Elements
{
	public class ImageBase<T> : ElementBase<T> where T : ImageBase<T>
	{
		public ImageBase() : base(HtmlTagNames.Img)
		{
		}

		/// <summary>
		/// Set the 'alt' attribute.
		/// </summary>
		/// <param name="alt">The value of the 'alt' attribute.</param>
		public virtual T Alt(string alt)
		{			
			Builder.MergeAttribute(HtmlTagAttributes.Alt, alt, true);
			return (T)this;
		}

		/// <summary>
		/// Set the 'src' attribute.
		/// </summary>
		/// <param name="value">The value of the 'src' attribute.</param>
		public virtual T Src(string value)
		{
			var src = HttpUtility.UrlPathEncode(value);

			Builder.MergeAttribute(HtmlTagAttributes.Src, src, true);
			return (T)this;
		}

		public virtual T Src(Uri uri)
		{
			if (uri != null)
			{
				return Src(uri.ToString());
			}
			return (T)this;
		}

		protected virtual bool HasSrc()
		{
			string value;
			if (Builder.Attributes.TryGetValue(HtmlTagAttributes.Src, out value))
			{
				if (!String.IsNullOrEmpty(value))
					return true;
			}

			return false;
		}

		protected virtual bool HasAlt()
		{
			string value;
			if (Builder.Attributes.TryGetValue(HtmlTagAttributes.Alt, out value))
			{
				if (!String.IsNullOrEmpty(value))
					return true;
			}

			return false;
		}
	}
}
